package com.base.myproject.client.HR.bcode;


import java.util.ArrayList;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.tools.Tools;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.base.myproject.client.ui.celleditor.BaseComboBoxX;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.VerticalPanel;


public class Rpt_BureauTotal extends BaseJDBCPanel{

	private Button btnQuery = null;
	private BaseComboBoxForm<BusiObject> department = null;
	private TextField<String> bureau = null;
	private TextField<String> bname = null;
	private TextField<String> employno = null;
	private BaseStore<BusiObject> store = null;
	private BaseGrid<BusiObject> grid;
	private BaseComboBoxX C_num6 = new BaseComboBoxX("name", "dictionary","where type='hr_ws'");
	private Button toExcel = new Button("导出到EXCEL");
	private String where = "";
	public Rpt_BureauTotal(){
		init();
	}
	
	public void init(){
		initWidget();
	}
	
	public void complete() {
		super.complete();
		print.addListener(Events.Select, getListener());
		toExcel.addListener(Events.Select, getListener());
		getToolbarll().add(print);
		getToolbarll().add(toExcel);
	}
	
	public void initWidget(){
		HorizontalPanel hp = new HorizontalPanel();
		hp.setSpacing(3);
		department = new BaseComboBoxForm<BusiObject>("id","name","Systeminfo","",true);
		btnQuery = new Button("查询");
		btnQuery.addListener(Events.OnClick, getListener());
		hp.add(new Label("部门："));
		hp.add(department);
		department.setWidth(127);
		bureau = new TextField<String>();
		bname = new TextField<String>();
		employno = new TextField<String>();
		hp.add(new Label("衣柜编号："));
		hp.add(bureau);
		hp.add(new Label("雇佣编码："));
		hp.add(employno);
		hp.add(new Label("姓名："));
		hp.add(bname);
		hp.add(new Label("状态："));
		hp.add(C_num6);
		hp.add(btnQuery);
		
		VerticalPanel vp = new VerticalPanel();
		vp.setSpacing(3);
		vp.add(hp);
		vp.add(getGrid());
		add(vp);
	}
	
	private BaseStore<BusiObject> getStore() {
		if (store == null) {
			createStore();
		}
		return store;
	}

	private BaseStore<BusiObject> createStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		
		type.addField("icode");
		type.addField("upcode");
		type.addField("bcode");
		type.addField("bname");
		type.addField("depname");
		type.addField("departmentName");
		type.addField("status");
		StoreInfo info = new StoreInfo();
		store = new BaseStore<BusiObject>(type, info);
		return store;
	}

	
	private BaseGrid<BusiObject> getGrid(){
		if(grid == null){
			createGrid();
		}
		return grid;
	}
	
	private BaseGrid<BusiObject> createGrid(){
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		
		ColumnConfig c1 = new ColumnConfig("upcode", "衣柜号", 60);
	    ColumnConfig c2 = new ColumnConfig("bcode", "雇佣编码", 100);
	    
	    ColumnConfig c3 = new ColumnConfig("bname", "姓名", 80);
	    ColumnConfig c4 = new ColumnConfig("depname", "小部门", 120);
	    ColumnConfig c5 = new ColumnConfig("departmentName", "大部门", 120);
	    ColumnConfig c6 = new ColumnConfig("status", "状态", 120);
	    
		columns.add(c1);
		columns.add(c2);
		columns.add(c3);
		columns.add(c4);
		columns.add(c5);
		columns.add(c6);
		ColumnModel cm = new ColumnModel(columns);
		
		grid = new BaseGrid<BusiObject>(getStore(), cm,false,false);
		Tools.setResize(grid, 0.69, 0.90);
		return grid;
	}
	
	int sync = 0;                                                      
	Listener<BaseEvent> defaultlistener;                               
                                                                     
	public Listener<BaseEvent> getListener() {                                
		if(defaultlistener!=null){                                                                
			return defaultlistener;                                        
		}                                                                
                                                                     
		defaultlistener = new Listener<BaseEvent>() {                    
			public void handleEvent(BaseEvent be) {                        
				sync++;                                                    
				try {                                                      
					if (sync == 1) {                                   
						if (be.getType() == Events.Select) { 
							if(be.getSource().equals(print)) {
								Window.open(GWT.getHostPageBaseURL() + "hr_Jsp/rptBureauTotal.jsp?" + URL.encode("data= and systemid like '" + department.getValueCode() + "%'" + where),"_blank","menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes");
							} else {
								Window.open(GWT.getHostPageBaseURL() + "hr_Jsp/rptBureauTotal.jsp?" + URL.encode("data= and systemid like '" + department.getValueCode() + "%'" + where + "&excel=1"),"_blank","menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes");
							}
							
						}else if(be.getType() == Events.OnBlur){
							
						}else if(be.getType() == Events.OnClick){
							if(be.getSource().equals(btnQuery)){
								String depCode = department.getValueCode();
								String bureaustr = bureau.getValue();
								String bnamestr = bname.getValue();
								String employnostr = employno.getValue();
								String status = C_num6.getRawValue();
								if(depCode == null || depCode == ""){
									MessageBox.alert("提示", "请选择部门", null);
									return;
								}
								
								where = "";
								
								if(bureaustr != null){
									where += " and upcode = '"+bureaustr+"'";
								}
								if(bnamestr != null){
									where += " and bcode.bname like '%25"+bnamestr+"%25'";
								}
								if(employnostr != null){
									where += " and employno like '%25"+employnostr+"%25'";
								}
								if(!status.equals("")){
									where += " and hr_wardrode_bcode.status = '"+status+"'";
								}
								String sql = "select bcode,bname,upcode,(select name from systeminfo where id=up_department) as departmentName,(select name from systeminfo where id=systemid) as depname,status from " +
										" (select up_department,employno as bcode,bcode.bname as bname,upcode,systemid,hr_wardrode_bcode.status as status " +
										" from hr_wardrode_bcode,bcode,bcode_job where bcode.bcode=hr_wardrode_bcode.bcode " +
										"and bcode.bcode=bcode_job.bcode and systemid like '%25"+depCode+"%25'"+where+") as a";
								grid.getStore().getStoreinfo().setSql(sql);
								grid.getStore().reload();
								grid.getView().refresh(true);
							}
						}                                          
					}                                                  
				} finally {                                                
					sync--;                                            
				}                                                          
			}                                                              
		};                                                               
		return defaultlistener;                                          
	}
}
